package com.cjy.controller;

import com.cjy.handler.NotFoundException;
import com.cjy.pojo.User;
import com.cjy.service.UserService;
import com.cjy.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService userService;

    @Value("${fastdfs_domaint}")
    private String fastdfsDomaint;

    @GetMapping
    public String loginPage(){
        return "admin/login";
    }

    /**
     * 登录接口
     * @param username 用户名
     * @param password 密码
     * @return 返回页面
     */
    @PostMapping("/login")
    public String login(@RequestParam(name = "username") String username,
                        @RequestParam(name = "password") String password,
                        HttpSession session,
                        RedirectAttributes attributes){
        User user = userService.checkUser(username, MD5Utils.code(password));
        if (user != null){
            user.setPassword(null);
            user.setRealAvatar(fastdfsDomaint + user.getAvatar());
            session.setAttribute("user",user);
            return "admin/index";
        }else{
            // 该方法的作用域是session，将数据跳转到页面后立即移除该对象
            attributes.addFlashAttribute("message","用户名和密码错误！");
            return "redirect:/admin";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/admin";
    }

    /**
     * 用户设置
     */
    @GetMapping("/info_show")
    public String showInfo(HttpSession session, User user, Model model){
        User one = (User) session.getAttribute("user");
        model.addAttribute("people",one);
        model.addAttribute("header",fastdfsDomaint);
        return "/admin/info_show";
    }

    @PostMapping("/updateUser")
    public String updateInfo(HttpSession session, User user, Model model){
        User one = (User) session.getAttribute("user");
        one.setRealAvatar(fastdfsDomaint+user.getAvatar());
        model.addAttribute("people",one);
        one.setNickname(user.getNickname());
        one.setUsername(user.getUsername());
        one.setUserDescription(user.getUserDescription());
        one.setWxQR(user.getWxQR());
        one.setZfbQR(user.getZfbQR());
        one.setAvatar(user.getAvatar());
        one.setUpdateTime(new Date());
        User updateUser = userService.updateUser(one.getId(), one);
        updateUser.setRealAvatar(fastdfsDomaint+updateUser.getAvatar());
        if (updateUser!=null){
            model.addAttribute("mag","更新成功！");
            session.setAttribute("user",updateUser);
            model.addAttribute("header",fastdfsDomaint);
            return "/admin/info_show";
        }else {
            throw new NotFoundException("更新数据丢失");
        }
    }
}
